package com.ztf.grouptask;

import java.util.List;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.task.IdentityLink;
import org.activiti.engine.task.Task;
import org.junit.Test;

/*
 * 当启动完流程实例以后，流程进入了
 *    <userTask id="usertask1" name="User Task" activiti:candidateUsers="张一,张二,张三,张四"></userTask>
 *    该节点
 *   在act_ru_identitylink表中插入了四行数据  candidate
 */
public class GroupTaskTest {

    @Test
    public void testDeploy(){
        //获取流程引擎
        ProcessEngine processEngine =  ProcessEngines.getDefaultProcessEngine();
        processEngine.getRepositoryService()
                .createDeployment()
                .addClasspathResource("com/itheima09/activiti5/grouptask/grouptask.bpmn")
                .deploy();
    }

    /**
     * 启动流程实例
     */
    @Test
    public void testStartProcessInstance(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        processEngine.getRuntimeService()
                .startProcessInstanceById("grouptask:1:2704");
    }

    /**
     * 根据任务的候选人查看组任务
     */
    @Test
    public void testQueryGroupTaskByCandidate(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        List<Task> tasks = processEngine.getTaskService()
                .createTaskQuery()
                .taskCandidateUser("张一")
                .list();
        for (Task task : tasks) {
            System.out.println(task.getName());
        }
    }

    /**
     * 根据组任务查看候选人
     */
    @Test
    public void testQueryCandidateByGroupTask(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        List<IdentityLink> identityLinks = processEngine.getTaskService()
                .getIdentityLinksForTask("2804");
        for (IdentityLink identityLink : identityLinks) {
            System.out.println(identityLink.getUserId());
        }
    }

    /**
     * 选举任何一个人接受该任务
     */
    @Test
    public void testClaimTask(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        processEngine.getTaskService()
                .claim("2804", "张一");
    }
}

